package org.eae.eldf.webapp.controller.prod;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eae.eldf.webapp.base.AbstractController;
import org.eae.eldf.webapp.vo.PageListVo;
import org.eae.eldf.entity.prod.ProdCategory;
import org.eae.eldf.entity.prod.ProdCategoryPurview;
import org.eae.eldf.framework.annotation.Token;
import org.eae.eldf.framework.pager.Pagination;
import org.eae.eldf.framework.persistence.Criteria;
import org.eae.eldf.framework.spring.SpringSecurityUtils;
import org.eae.eldf.service.prod.ProdCategoryPurviewService;
import org.eae.eldf.service.prod.ProdCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/prod")
public class ProdCategoryController extends AbstractController<ProdCategory> {
	private ProdCategoryService prodCategoryService;
	
	private ProdCategoryPurviewService prodCatecopyPurviewService;
	@Autowired
	public void setProdCategoryService(ProdCategoryService prodCategoryService) {
		this.prodCategoryService = prodCategoryService;
	}
	@Autowired
	public void setProdCatecopyPurviewService(
			ProdCategoryPurviewService prodCatecopyPurviewService) {
		this.prodCatecopyPurviewService = prodCatecopyPurviewService;
	}
	
	
	/**
	 * 跳转到产品类别信息页面
	 * @param modelMap
	 * @param id
	 * @param searchValue
	 * @return
	 */
	@RequestMapping(value="/category/form")
	@Token(save=true)
	public String toProdCategoryPage(ModelMap modelMap,@RequestParam(required=false) Long id,
			@RequestParam(required=false) String searchValue){
		ProdCategory prodCategory=null;
		if(id!=null){
			prodCategory=prodCategoryService.get(ProdCategory.class, id);
		}
		else {
			prodCategory=new ProdCategory();
		}
		modelMap.addAttribute("prodCategory",prodCategory);
		modelMap.addAttribute("searchValue",searchValue);
		return "/prod/category/form";

	}

	
	
	/**
	 * 查询列表
	 * 
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/category/list")
	public String initPage(@RequestParam(required = false) String searchValue,
			ModelMap model, HttpServletRequest request, HttpServletResponse response) {
		return "/prod/category/list";
	}
	
	@RequestMapping(value = "/category/list_json")
	public @ResponseBody PageListVo<ProdCategory> pageList(
			HttpServletRequest request, 
			HttpServletResponse response,
			@RequestParam(required = false) Integer page,
			@RequestParam(required = false) Integer rows,
			@RequestParam(required = false) String searchValue){
		Pagination<ProdCategory> pager = super.initPagination(request);
		pager = prodCategoryService.queryPageList(pager, searchValue);
		return pageListJson(pager);
	}
	/**
	 * 产品类别插入
	 * 
	 * @param id
	 * @param categoryName
	 * @param categoryDesc
	 * @param createUser
	 * @param modelMap
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/category/add")
	public ModelAndView insert(@RequestParam(required = false) Long id,
			ModelAndView modelAndView,
			@RequestParam(required = false) String categoryName,
			@RequestParam(required = false) String categoryDesc,
			ModelMap modelMap, HttpServletRequest request) {
		modelAndView.setViewName("forward:/prod/category/list");
		prodCategoryService.insert(Criteria.create(ProdCategory.class)
				.set("categoryName", categoryName)
				.set("categoryDesc", categoryDesc)
				.set("createTime", new Date()).set("createUser", SpringSecurityUtils.getCurrentUserName()));
		modelAndView.addObject("success", true);
		return modelAndView;
	}

	/**
	 * 产品类别编辑
	 * 
	 * @param id
	 * @param categoryName
	 * @param categoryDesc
	 * @param lastUpdateUser
	 * @param modelMap
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/category/edit")
	public ModelAndView update(@RequestParam(required = false) Long id,
			ModelAndView modelAndView,
			@RequestParam(required = false) String categoryName,
			@RequestParam(required = false) String categoryDesc,
			ModelMap modelMap, HttpServletRequest request) {
		modelAndView.setViewName("forward:/prod/category/list");
		prodCategoryService.update(Criteria.create(ProdCategory.class)
				.set("categoryName", categoryName)
				.set("categoryDesc", categoryDesc)
				.set("lastUpdateTime", new Date())
				.set("lastUpdateUser", SpringSecurityUtils.getCurrentUserName())
				.where("prodCategoryId", new Object[] { id }));
		modelAndView.addObject("success", true);
		return modelAndView;
	}

	/**
	 * 产品类别删除
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/category/del")
	public @ResponseBody
	String delete(@RequestParam Long id) {
		try {
			prodCatecopyPurviewService.delete(Criteria.create(ProdCategoryPurview.class).where("prodCategoryId", new Object[]{id}));
			
			prodCategoryService.delete(ProdCategory.class, id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "success";
	}

}
